package template_funcs

import (
	"app/app_src/framework/i18n"
	template_funcs_util "app/app_src/framework/template/funcs/util"
	"strings"
)

type LibLangFunc func(url string) string

// 根据语言代码生成第三方库脚本的多语言文件的URL
func NewLibLangFunc(mappingUrls map[string]string) LibLangFunc {
	result := func(url string) string {
		mappedUrl := template_funcs_util.GetMappedUrl(mappingUrls, url)
		var oldSuffix string

		if strings.HasSuffix(mappedUrl, ".min.js") {
			oldSuffix = ".min.js"
		} else {
			oldSuffix = ".js"
		}

		langCode := i18n.GetLangCode()
		newSuffix := "-" + langCode + oldSuffix
		newUrl := strings.Replace(mappedUrl, oldSuffix, newSuffix, 1)

		return newUrl
	}

	return result
}
